$(document).ready(function(){
    applyDatepickerSf();
    $('body').on("change","#srcData_plant",{field: 'plant'},updateForm);
    $('body').on("change","#srcData_powerdev",{field: 'powerdev'},updateForm);
    
    applySelect2Side();
});

function updateForm(event){
    
  
    $('#loader').show();
    
    if (event.data.field==='plant'){
        var par={ quest:"srcData", plant: $('#srcData_plant').val()};
    }else if (event.data.field==='powerdev'){
        var par={ quest:"srcData", plant: $('#srcData_plant').val(), powerdev: $('#srcData_powerdev').val() };
    }   
    
    var prevSelection=$('#srcData_variables').find("option:selected");

    $('#sourceForm').load(window.location.pathname,par,function(){
        
        $('#loader').hide();
        
        applySelect2Side(prevSelection);
            
        applyDatepickerSf();
    });
    
}

function applySelect2Side(prevSelection){
    
        $('#srcData_variables').multiselect2side({
            search: 'Search: '
        });
        if ( prevSelection !== undefined){
            prevSelection.each(function(){
            $('#srcData_variables').multiselect2side('addOption', {
                name: $(this).text(), 
                value: $(this).text(), 
                selected: true});
            }); 
        }

        $('#srcDatams2side__sx').attr("size",10);
        $('#srcDatams2side__dx').attr("size",10);
    
}